#!/bin/bash

echo "1. 环境清理"
mkdir -p config
mkdir -p crypto-config
rm -fr ./config/*
rm -fr ./crypto-config/*
echo "清理完毕"
echo ""

echo "2. 生成组织基本结构"
cryptogen generate --config=./crypto-config.yaml
echo ""

echo "3. 生成初始区块"
configtxgen -profile TwoOrgsOrdererGenesis -outputBlock ./config/genesis.block
echo ""

echo "4. 生成通道配置文件"
configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./config/channel.tx -channelID assetschannel
echo ""

echo "5. 生成锚节点更新文件"
configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./config/Org1MSPanchors.tx -channelID assetschannel -asOrg Org1MSP
echo ""

echo "6. 启动各容器节点"
docker-compose -f docker-compose-cli.yaml up -d
#sleep 10
#echo "等待10s"
echo ""

echo "7. 创建通道"
docker exec cli peer channel create -o orderer.lottery.cn:7050 -c assetschannel -f ./config/channel.tx
echo ""

echo "8. 将各节点加入到通道中"
docker exec cli peer channel join -b assetschannel.block
docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_ADDRESS=node1.organization1.lottery.cn:7051" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/organization1.lottery.cn/users/Admin@organization1.lottery.cn/msp" cli peer channel join -b assetschannel.block
docker exec -e "CORE_PEER_LOCALMSPID=Org2MSP" -e "CORE_PEER_ADDRESS=node1.organization2.lottery.cn:7051" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/organization2.lottery.cn/users/Admin@organization2.lottery.cn/msp" cli peer channel join -b assetschannel.block
docker exec -e "CORE_PEER_LOCALMSPID=Org2MSP" -e "CORE_PEER_ADDRESS=node2.organization2.lottery.cn:7051" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/organization2.lottery.cn/users/Admin@organization2.lottery.cn/msp" cli peer channel join -b assetschannel.block
docker exec -e "CORE_PEER_LOCALMSPID=Org3MSP" -e "CORE_PEER_ADDRESS=node1.organization3.lottery.cn:7051" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/organization3.lottery.cn/users/Admin@organization3.lottery.cn/msp" cli peer channel join -b assetschannel.block
docker exec -e "CORE_PEER_LOCALMSPID=Org3MSP" -e "CORE_PEER_ADDRESS=node2.organization3.lottery.cn:7051" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/organization3.lottery.cn/users/Admin@organization3.lottery.cn/msp" cli peer channel join -b assetschannel.block
echo ""

echo "9. 在背书安装链码"
docker exec cli peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/school-lottery
docker exec -e "CORE_PEER_LOCALMSPID=Org2MSP" -e "CORE_PEER_ADDRESS=node2.organization2.lottery.cn:7051" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/organization2.lottery.cn/users/Admin@organization2.lottery.cn/msp" cli peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/school-lottery
docker exec -e "CORE_PEER_LOCALMSPID=Org3MSP" -e "CORE_PEER_ADDRESS=node2.organization3.lottery.cn:7051" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/organization3.lottery.cn/users/Admin@organization3.lottery.cn/msp" cli peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/school-lottery
echo ""

echo "10. 实例化"
docker exec cli peer chaincode instantiate -o orderer.lottery.cn:7050 -C assetschannel -n mycc -l golang -v 1.0 -c '{"Args":["init"]}' -P "AND ('Org1MSP.peer','Org2MSP.peer','Org3MSP.peer')"
#sleep 10
#echo "等待10s"
echo ""

echo "11. 链码交互"
docker exec -e "CORE_PEER_LOCALMSPID=Org2MSP" -e "CORE_PEER_ADDRESS=node2.organization2.lottery.cn:7051" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/organization2.lottery.cn/users/Admin@organization2.lottery.cn/msp" cli peer chaincode query -C assetschannel -n mycc -c '{"Args":["query","a"]}'
docker exec -e "CORE_PEER_LOCALMSPID=Org3MSP" -e "CORE_PEER_ADDRESS=node2.organization3.lottery.cn:7051" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/organization3.lottery.cn/users/Admin@organization3.lottery.cn/msp" cli peer chaincode query -C assetschannel -n mycc -c '{"Args":["query","b"]}'